import {defineStore} from "pinia"
import router from "@/routes/index.js"

export const Tabs = defineStore("Tabs",{

    state:()=>{
            return {
                tabList:[]
            }
    },
    actions:{
        // 添加tabList
        addTabsList(data){
            // console.log(data,"添加tabs")
            
            if(this.tabList.every(item=>item.path!==data.path)){
                this.tabList.push(data)
            }
        },

        removeTabsList(tabPath,isCurrent){
            const tabsMenuList = this.tabList
            if(isCurrent){
                // console.log("判断router走向了")
                tabsMenuList.forEach((item,index)=>{
                    if(item.path !==tabPath)return
                    // console.log(index)
                    const nestTab = tabsMenuList[index+1] || tabsMenuList[index-1];
                    // console.log(nestTab)
                    if(!nestTab)return
                    router.push(nestTab.path)
                    // console.log(tabsMenuList[index+1],"第一个fesfes")
                    // console.log(tabsMenuList[index-1],"第二个")
                })

            }
            this.tabList = this.tabList.filter(item=>item.path !==tabPath)
            // console.log(tabPath,isCurrent)
        }
        

    },
    getters:{

    },
    persist:{
        storage:localStorage,
        paths:["tabList"]
    }

})